package com.sansecy.base.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.sansecy.base.FullScreenUtils;
import com.sansecy.base.click.OnCustomClickListener;
import com.sansecy.base.file.OnFileClickListener;

import org.jetbrains.annotations.NotNull;

public class BaseDialogFragment extends DialogFragment {
    public final <T extends View> T findViewById(@IdRes int id) {
        return getView().findViewById(id);
    }

    private static final String TAG = "BaseDialogFragment";
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getContext()){
            @Override
            protected void onStart() {
                super.onStart();
                Window window = getDialog().getWindow();
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                WindowManager.LayoutParams attributes = window.getAttributes();
                DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
                if (displayMetrics.heightPixels > displayMetrics.widthPixels) {
                    attributes.gravity = Gravity.BOTTOM;
                    attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
                    attributes.height = (int) (displayMetrics.heightPixels * 0.5);
                } else {
                    attributes.gravity = Gravity.END;
                    attributes.width = (int) (displayMetrics.widthPixels * 0.5);
                    attributes.height = ViewGroup.LayoutParams.MATCH_PARENT;
                }
                window.setAttributes(attributes);
            }
            @Override
            public void onWindowFocusChanged(boolean hasFocus) {
                super.onWindowFocusChanged(hasFocus);
                if (hasFocus) {
                    FullScreenUtils.setStickFullScreen(getWindow().getDecorView());
                }
            }
        };

        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }


    protected OnFileClickListener mOnFileClickListener;

    public void setOnFileClickListener(OnFileClickListener onFileClickListener) {
        mOnFileClickListener = onFileClickListener;
    }

    protected OnCustomClickListener mOnClickListener;

    public void setOnClickListener(OnCustomClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }
}
